package jers.example;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.RemoteException;

import jers.Callback;
import jers.JERS;
import jers.Job;
import jers.Manager;
import jers.Result;
import jers.Service;
import jers.util.SecurityUtil;


public class DownloadFile implements Job, Callback {

	private static final long serialVersionUID = 3905944048207576200L;
	
	private Result results;
	private String fileName;

	public DownloadFile(String fileName) {
		this.results = JERS.factory.newResult();
		this.fileName = fileName;
	}
	
	public Result results() {
		return this.results;
	}
	
	public void run() {
		byte[] file = DownloadFile.readFile(this.fileName);
		this.results.set("file", file);
	}

	public static byte[] readFile(String name) {
		byte[] fileBytes = null;
		try {
			File f = new File(name);
			if (!f.exists()) {
				return null;
			}
			
			fileBytes = new byte[(int) f.length()];
			
			BufferedInputStream buffer = new BufferedInputStream(
					new FileInputStream(f));
			buffer.read(fileBytes);
			buffer.close();
						
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileBytes;
	}
	
	public static boolean writeFile(String fileName, byte[] fileBytes) {
		boolean created = false;
		try {
			File f = new File(fileName);
			if (fileBytes == null || fileBytes.length == 0) { 
				return false;
			}
			
			BufferedOutputStream buffer = new BufferedOutputStream(
					new FileOutputStream(f));
			buffer.write(fileBytes);
			buffer.flush();
			buffer.close();
			
			if (f.exists()) {
				created = true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return created;
	}
	
	public static void main(String[] args) {
		SecurityUtil.setupSecurityPolicy();

		String server = "localhost";
		if (args.length > 0 && args[0] != null) {
			server = args[0];
		}
		try {
			DownloadFile download = new DownloadFile("/tmp/test.txt");
			Manager manager = JERS.factory.newManager();
			Service service = manager.getService(server);

			// Sincrono
			Result result = service.execute(download);
			
			// Assincrono
			service.dispatch(download, download);
			
			Object data = result.get("file");
			if (data != null) {
				DownloadFile.writeFile("/tmp/test2.txt", (byte[])data);
				System.out.println(">> Arquivo criado!");
			} else {
				System.out.println(">> Arquivo nulo!"); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void call(Result result) throws RemoteException {
		System.out.println(">> Arquivo recebido!"); 
		try {
			Object data = result.get("file");
			if (data != null) {
				DownloadFile.writeFile("/tmp/test3.txt", (byte[])data);
				System.out.println(">> Arquivo criado!");
			} else {
				System.out.println(">> Arquivo nulo!"); 
			}
			JERS.systemExit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
